<template>
    <div class="apiCacheContainer">
        <div class="searchContainer">
            <ServiceInstanceSearch />
        </div>
        <div>
            <el-table
                :data="serviceInstanceApplicationDataList"
                style="width: 100%; margin-bottom: 20px; margin-top: 20px;"
                row-key="address"
                border
                :show-overflow-tooltip="true"
                class="tableContainer"
                :span-method="tableSpanMethod"
                max-height="70vh"
            >
                <el-table-column prop="id" label="微服务ID" />
                <el-table-column prop="applicationName" label="微服务名称" />
                <el-table-column prop="instanceNum" label="实例数量" />
                <el-table-column prop="address" label="实例IP" />
                <el-table-column prop="healthy" label="健康状态" />
                <el-table-column prop="serviceName" label="微服务实例信息" />
                <el-table-column prop="weight" label="权重" />
                <el-table-column prop="metadata" label="元信息" />
            </el-table>
        </div>
    </div>
</template>

<script name="ServiceInstanceManage" setup lang="ts">
import ServiceInstanceSearch from "@/components/serviceManage/serviceInstanceManage/ServiceInstanceSearch.vue";
import {applicationNacosInfoQuery} from "@/request/service";
import {createTag} from "@/utils/ywuCommon";
import {computed, ref} from "vue";
import {useRoute} from "vue-router";
import {useServiceInstanceStore} from "@/stores/serviceManage/serviceInstance";

let serviceForm = ref({
    id: '',
    applicationName: ''
});

let serviceInstanceApplicationDataList = computed(() => {return useServiceInstanceStore().serviceInstanceApplicationDataList});
let serviceInstanceApplicationSearchData = computed(() => {return useServiceInstanceStore().serviceInstanceApplicationSearchData});

// 查询配置中心信息
applicationNacosInfoQuery(serviceInstanceApplicationSearchData.value).then(res => {
    if (res["code"] === '200') {
        useServiceInstanceStore().resetServiceInstanceApplicationDataList(res.data);
    }
})

// 创建标签
let route = useRoute();
createTag(route);

function tableSpanMethod({ row, column, rowIndex, columnIndex }) {
    let dataList = useServiceInstanceStore().serviceInstanceApplicationDataList;
    let dataCont = {}
    let dataRowIndex = {}
    dataList.forEach((res,index) => {
        dataCont[res.applicationName + "count"] = dataCont[res.applicationName + "count"] === undefined ? 1 : dataCont[res.applicationName + "count"] + 1;
        if (dataRowIndex[res.applicationName + "rowIndex"] === undefined) {
            dataRowIndex[res.applicationName + "rowIndex"] = index
        }
    })
    if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2) {
        let rowApplication = row.applicationName
        let rowApplicationIndex = dataRowIndex[rowApplication + "rowIndex"]
        if (rowApplicationIndex === rowIndex) {
            let count = dataCont[rowApplication + "count"]
            return {
                rowspan: count,
                colspan: 1,
            }
        } else {
            return {
                colSpan: 1,
                rowSpan: 1
            }
        }
    }
}
</script>

<style scoped lang="less">
.apiCacheContainer {
    width: 98%;
    height: 100%;
    margin: 0.2%;
    .searchContainer {
        width: 100%;
        margin-top: 1%;
    }
    .tableContainer {
        margin-left: 1%;
    }
    .insertContainer {
        margin-left: 1%;
        margin-top: 0.7%;
    }
}
</style>